//
//  MXPromptView.m
//  MXLoginMethodsDomo
//
//  Created by 马彦 on 16/9/29.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXPromptView.h"
#import "MXGraphicsView.h"

/**字体距离边缘间距*/
#define EdgeSpacing 40
/**字体显示的大小*/
#define TitleFont 14
/**子标题显示的大小*/
#define MessageFount 12

/**图形字体显示的大小*/
#define GrapicsTitle 17

#define ShowTime 1.0

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface MXPromptView ()
@property (nonatomic, strong) UILabel *titleLbl;

@property (nonatomic, strong) UILabel *messageLbl;

@end

@implementation MXPromptView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
        [self configBackgroundUI];
    }
    return self;
}

- (void)configBackgroundUI {
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGRect rect = window.bounds;
    self.frame = rect;
    
    [window addSubview:self];
}


/**
 显示提示框，有时间限制
 */
- (void)showAlert {
     self.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:0.5 animations:^{
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
       
        [window addSubview:self];
         self.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ShowTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self removeFromSuperview];
        });
    }];
}


/**
 显示提示框，无时间限制
 */
- (void)showAlertCancelTime {
    self.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:0.5 animations:^{
        
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        [window addSubview:self];
        self.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
        self.block();
    }];
}

/**
 手势解锁

 @param view  显示视图
 @param title 标题
 */
- (void)promptViewAlertViewWithView:(UIView *)view Title:(NSString *)title  {

    NSDictionary *titleDict = @{NSFontAttributeName : [UIFont systemFontOfSize:TitleFont], NSForegroundColorAttributeName : [UIColor blackColor]};
    CGSize titleSize = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:titleDict context:nil].size;
    
   
    CGFloat contentViewW = titleSize.width + EdgeSpacing;
    CGFloat contentViewH = titleSize.height + EdgeSpacing;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentViewW, contentViewH)];
    contentView.center = self.center;
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = 10;
    [self addSubview:contentView];
    
    
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(EdgeSpacing * 0.5, EdgeSpacing * 0.5, titleSize.width, titleSize.height)];
    titleLbl.text = title;
    titleLbl.font = [UIFont systemFontOfSize:TitleFont];
    titleLbl.textColor = [UIColor blackColor];
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:titleLbl];
 
    [self showAlert];
}

/**
 指纹密码提示

 @param view  显示框
 @param title 提示内容
 @param style 显示图案
 */
- (void)promptViewAlertViewWithView:(UIView *)view andTitle:(NSString *)title andStyle:(PromptViewStyle)style {
   
    NSDictionary *titleDict = @{NSFontAttributeName : [UIFont systemFontOfSize:GrapicsTitle], NSForegroundColorAttributeName : [UIColor blackColor]};
    CGSize titleSize = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:titleDict context:nil].size;
    
    
    CGFloat contentViewW = titleSize.width + EdgeSpacing;
    CGFloat contentViewH = titleSize.width + EdgeSpacing;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, contentViewW, contentViewH)];
    contentView.center = self.center;
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = 10;
    [self addSubview:contentView];
    
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(EdgeSpacing * 0.5, CGRectGetHeight(contentView.frame) - titleSize.height - 10, titleSize.width, titleSize.height)];
    titleLbl.text = title;
    titleLbl.font = [UIFont systemFontOfSize:GrapicsTitle];
    titleLbl.textColor = [UIColor blackColor];
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:titleLbl];
    
    CGFloat graphicsViewW = titleSize.width;
    CGFloat graphicsViewH = titleSize.width;
    CGFloat graphicsViewX = (CGRectGetWidth(contentView.frame) - graphicsViewW) * 0.5;
    CGFloat graphicsViewY = (CGRectGetHeight(contentView.frame) - graphicsViewH - CGRectGetHeight(titleLbl.frame) - 10) * 0.5;
    MXGraphicsView * graphicsView = [[MXGraphicsView alloc] initWithFrame:CGRectMake(graphicsViewX, graphicsViewY, graphicsViewW, graphicsViewH) style:PromptViewStyleSuccessful];
    
    [contentView addSubview:graphicsView];
    
    [self showAlert];
}


@end
